﻿using System;
using System.Collections.Generic;

namespace OS.Common.Core
{
    /// <summary>
    /// 分页实体扩展
    /// </summary>
    public static class PageListResultMap
    {
        /// <summary>
        /// 转化pageList列表实体
        /// </summary>
        /// <typeparam name="TParameter">动态参数</typeparam>
        /// <typeparam name="TResult">动态返回结果类型</typeparam>
        /// <param name="pageList">分页数据</param>
        /// <param name="convertFunc">转化方法</param>
        /// <returns></returns>
        public static PageListResult<TResult> ConvertToPageList<TParameter, TResult>(
            this PageListResult<TParameter> pageList, Func<TParameter, TResult> convertFunc)
            where TParameter : class, new() where TResult : class, new()
        {
            if (convertFunc == null)
                throw new ArgumentNullException("convertFunc", "转化方法不能为空！");

            List<TResult> list = null;
            if (pageList.Data != null)
                list = pageList.Data.ConvertAll(e => convertFunc(e));

            return new PageListResult<TResult>(pageList.Total, list);
        }
    }
}